package top.lingkang.finalgateway.core.ssl;


import top.lingkang.finalgateway.utils.SslUtils;

import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLEngine;
import java.io.FileInputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.KeyStore;
import java.util.Arrays;

/**
 * @author lingkang
 * Created by 2024/5/10
 */
public class SSLContextFactory {
    public static SSLContext getSslContext() throws Exception {
        char[] passArray = "5179x8e8".toCharArray();
        SSLContext sslContext = SSLContext.getInstance("TLSv1");
        KeyStore ks = KeyStore.getInstance("JKS");
        //加载keytool 生成的文件
        FileInputStream inputStream = new FileInputStream("C:\\Users\\Administrator\\Desktop\\temp\\key\\jks\\lingkang.top.jks");
        ks.load(inputStream, passArray);
        KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        kmf.init(ks, passArray);
        sslContext.init(kmf.getKeyManagers(), null, null);
        inputStream.close();
        return sslContext;
    }


    public static void main(String[] args) throws Exception{
        SSLContext sslContext = SslUtils.getSslContext(
                "5179x8e8",
                Files.newInputStream(Paths.get("C:\\Users\\Administrator\\Desktop\\temp\\key\\jks\\lingkang.top.jks")));
        SSLEngine engine = sslContext.createSSLEngine();
        engine.setUseClientMode(false);
        System.out.println(Arrays.toString(engine.getSupportedProtocols()));
        System.out.println(Arrays.toString(engine.getEnabledProtocols()));
        System.out.println(Arrays.toString(engine.getEnabledCipherSuites()));

    }
}
